cmake_minimum_required(VERSION 3.15)

project(Workspace
  VERSION 0.95
  LANGUAGES C)
set(CMAKE_C_STANDARD 11)

include(CheckIncludeFiles)
include(CheckFunctionExists)
include(CheckSymbolExists)

check_include_files("bsd/string.h" HAVE_BSD_STRING_H)
if (HAVE_BSD_STRING_H)
  set(CMAKE_REQUIRED_LIBRARIES "-lbsd")
  check_symbol_exists(strlcat "bsd/string.h" STRLCAT)
  check_symbol_exists(strlcpy "bsd/string.h" STRLCPY)
  if (STRLCAT AND STRLCPY)
    set (HAVE_STRLCAT 1)
  endif()
endif()

check_include_files("ctype.h" HAVE_CTYPE_H)
check_include_files("dlfcn.h" HAVE_DLFCN_H)
check_include_files("fcntl.h" HAVE_FCNTL_H)
check_include_files("inttypes.h" HAVE_INTTYPES_H)
check_include_files("libintl.h" HAVE_LIBINTL_H)
check_include_files("limits.h" HAVE_LIMITS_H)
check_include_files("memory.h" HAVE_MEMORY_H)
check_include_files("fcntl.h" HAVE_FCNTL_H)
check_include_files("sys/inotify.h" HAVE_INOTIFY_H)
if (HAVE_INOTIFY_H)
  check_function_exists(inotify_init HAVE_INOTIFY)
endif()
check_include_files("inttypes.h" HAVE_INTTYPES_H)
check_include_files("libintl.h" HAVE_LIBINTL_H)
check_include_files("limits.h" HAVE_LIMITS_H)
check_include_files("malloc.h" HAVE_MALLOC_H)
if (HAVE_MALLOC_H)
  check_function_exists(mallinfo HAVE_MALLINFO)
endif()
check_include_files("memory.h" HAVE_MEMORY_H)
check_include_files("poll.h" HAVE_POLL_H)
if (HAVE_POLL_H)
  check_function_exists(poll HAVE_POLL)
endif()
check_include_files("memory.h" HAVE_MEMORY_H)
check_include_files("stdint.h" HAVE_STDINT_H)
check_include_files("stdlib.h" HAVE_STDLIB_H)
check_include_files("stdnoreturn.h" HAVE_STDNORETURN)
check_include_files("strings.h" HAVE_STRINGS_H)
check_include_files("string.h" HAVE_STRING_H)
check_include_files("syslog.h" HAVE_SYSLOG_H)
if (HAVE_SYSLOG_H)
  set(HAVE_SYSLOG 1)
endif()
check_include_files("sys/ioctl.h" HAVE_SYS_IOCTL_H)
check_include_files("sys/stat.h" HAVE_SYS_STAT_H)
check_include_files("sys/wait.h" HAVE_SYS_WAIT_H)
check_include_files("unistd.h" HAVE_UNISTD_H)
check_include_files("sys/types.h" HAVE_SYS_TYPES_H)

check_function_exists(gethostname HAVE_GETHOSTNAME)
check_function_exists(mkstemp HAVE_MKSTEMP)
check_function_exists(secure_getenv HAVE_SECURE_GETENV)
check_function_exists(select HAVE_SELECT)
check_function_exists(setsid HAVE_SETSID)
check_function_exists(strcasecmp HAVE_STRCASECMP)
check_function_exists(strncasecmp HAVE_STRNCASECMP)
check_function_exists(sysconf HAVE_SYSCONF)
check_function_exists(vprintf HAVE_VPRINTF)

check_include_files("math.h" HAVE_MATH_H)
if (HAVE_MATH_H)
  set(CMAKE_REQUIRED_LIBRARIES "-lm")
  check_function_exists(atan HAVE_ATAN) # -lm
  check_function_exists(sqrt HAVE_SQRT) # -lm
  check_function_exists(cosf HAVE_COSF) # -lm
  check_function_exists(sinf HAVE_SINF) # -lm
  check_function_exists(powf HAVE_POWF) # -lm
  if (HAVE_ATAN AND HAVE_SQRT AND HAVE_COSF AND HAVE_SINF AND HAVE_POWF)
    set(HAVE_FLOAT_MATHFUNC 1)
  endif()
  check_symbol_exists(M_PI "math.h" HAVE_M_PI)
  if (HAVE_M_PI)
    set(WM_PI 1)
    set(M_PI_VALUE "(M_PI)")
  endif()
endif()

set(CMAKE_REQUIRED_LIBRARIES "-lX11")
check_symbol_exists(XConvertCase "X11/Xutil.h" HAVE_XCONVERTCASE)
check_symbol_exists(XInternAtoms "X11/Xlib.h" HAVE_XINTERNATOMS)

check_include_files("X11/extensions/shape.h" USE_XSHAPE)
check_include_files("X11/extensions/Xrandr.h" USE_XRANDR)
check_include_files("X11/XKBlib.h" USE_XKB)

configure_file(config.h.in ../config.h)

